package com.example.dingtalkwebproject.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Service
public class VideoStorageService {
    // 配置存储路径（建议通过@Value从配置文件读取）
//    private static final String UPLOAD_DIR = "D:/path/to/your/upload/directory/";
    private static final String UPLOAD_DIR = "/data/sharefile/W_BL_SHIP_HD/";

    /**
     * 存储视频文件并返回唯一文件名
     */
    public String storeVideo(MultipartFile file) throws IOException {
        // 生成唯一文件名（UUID + 时间戳 + 后缀）
        String originalName = file.getOriginalFilename();
        String fileExtension = originalName.substring(originalName.lastIndexOf("."));
        String uniqueName = UUID.randomUUID().toString() + "_" +
                            System.currentTimeMillis() + fileExtension;

        // 创建目标目录
        Path uploadPath = Paths.get(UPLOAD_DIR);
        if (!Files.exists(uploadPath))  {
            Files.createDirectories(uploadPath);
        }

        // 存储文件
        Path targetPath = uploadPath.resolve(uniqueName);
        file.transferTo(targetPath);

        // 返回带有路径前缀的文件名
        return "/W_BL_SHIP_HD/" + uniqueName;
    }

    /**
     * 获取视频存储的绝对路径
     */
    public String getStoragePath() {
        return UPLOAD_DIR;
    }
}
